<template>
  <el-dialog :width="'30%'" v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
             label-width="120px">
      <el-form-item label="线路名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="线路名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="所属项目">
        {{ dataForm.projectName }}
      </el-form-item>
      <el-form-item label="施工日期" prop="startDateStr">
        <div class="block">
          <el-date-picker
            v-model="dataForm.startDateStr"
            type="date"
            placeholder="选择日期" value-format="YYYY-MM-DD">
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item label="备注" prop="notes">
        <el-input v-model="dataForm.notes" placeholder="备注" clearable></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {reactive, ref} from "vue";
import baseService from "@/service/baseService";
import {ElMessage} from "element-plus";
import {useRouter} from "vue-router";

const router = useRouter();
const emit = defineEmits(["refreshDataList"]);
const visible = ref(false);
const dataFormRef = ref();
let dataForm = reactive({
  id: '', name: '', projectId: '', createDate: '', startDate: '', notes: '', projectName: "", startDateStr: ""
});

const rules = ref({
  name: [
    {required: true, message: '线路名称不能为空', trigger: 'blur'}
  ],
  startDateStr: [
    {required: true, message: '施工日期不能为空', trigger: 'blur'}
  ]
});

const init = (row?: {}) => {
debugger
  if ("" == row.projectId) {
    ElMessage.error({
      message: '请重新选择项目', duration: 500,
      onClose: () => {
        router.replace("/sysproject/project");
      }
    });
    return
  }else if(undefined != row.name){
    debugger
    Object.assign(dataForm, row);
    dataForm.startDateStr = row.startDate;
  }else if(undefined == row.name){
    dataForm.projectName = row.projectName;
    dataForm.projectId = row.projectId;
    dataForm.name = "";
    dataForm.notes = "";
    dataForm.startDateStr = "";
  }
  visible.value = true;

};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/sysproject/line/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/sysproject/line", dataForm).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
